package servlets;

import java.io.IOException;
import java.util.Vector;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Mesto;
import model.Povlastica;
import model.Prevoznik;
import model.PutnaDuz;
import model.User;
import model.Util;

public class TimeTableServlet extends HttpServlet {

	private static final long serialVersionUID = -7310304827588123988L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {

		resp.setContentType("text/html; charset=utf-8");

		// uzimamo iz sesije podatke
		HttpSession session = req.getSession(true);
		User user = (User) session.getAttribute("user");

		String showTrip = req.getParameter("showTrip");
		String fromTown = req.getParameter("fromTown");
		String toTown = req.getParameter("toTown");

		// ako korisnik nije logovan, vracamo ga na pocetnu stranicu
		if (user == null || !user.isLoggedIn()) {
			req.getRequestDispatcher("WebContent/logIn.jsp").forward(req, resp);
			return;
		}

		Prevoznik prevoznik = (Prevoznik) session.getAttribute("prevoznik");

		// ovde smo dosli posle logovanja, ucitacemo sve povlastice za korisnika
		// ucitavaju se samo jedanput
		@SuppressWarnings("unchecked")
		Vector<Povlastica> allBens = (Vector<Povlastica>) session
		.getAttribute("allBens");
		if (allBens == null) {
			allBens = Util.getBensForCarrier(prevoznik);
			session.setAttribute("allBens", allBens);
		}

		// ako je logovan ucitavamo SAMO sva mesta
		System.out.println("\n\tUcitavamo iz baze mesta...");

		Vector<Mesto> mesta = Util.getAllEntitiesFromTable(Mesto.class);
		session.setAttribute("allTowns", mesta);

		System.out.println("\n\tUcitano: " + mesta.size() + " mesta");

		// ako trebamo da pokazemo i linije, trebamo osveziti samo linije
		if (showTrip != null && showTrip.equals("true")) {
			// trebamo prikazati putanju
			System.out.println("\n\tIzdvajamo podatke za prikaz");

			Mesto mfromTown = Util.getMestoFromName(fromTown);
			System.out.println("from="+mfromTown.getNaziv());
			Mesto mToTown = Util.getMestoFromName(toTown);
			System.out.println("to="+mToTown.getNaziv());
			session.setAttribute("mfromTown", mfromTown);
			session.setAttribute("mToTown", mToTown);

			System.out.println("Vadimo red voznje za prevoznika: "
					+ prevoznik.getNaziv());
			Vector<PutnaDuz> trips = Util.getAllTripsByCarrier(prevoznik);

			//System.out.println("Posle filtriranja: " + trips.size());

			//trips = Util.getPaths(mfromTown, mToTown, trips, Util.NAJBRZI);
			session.setAttribute("allTrips", trips);

			System.out.println("\n\tUcitano: " + trips.size() + " putnih duzi");

			// redirektovacemo na stranicu sa redom voznje
			RequestDispatcher disp = req
			.getRequestDispatcher("WebContent/timeTable.jsp");
			disp.forward(req, resp);
			return;
		}

		// trebamo da ucitamo ponovo sve putne duzi
		// Prevoznik prevoznik = (Prevoznik) session.getAttribute("prevoznik");
		Vector<PutnaDuz> trips = Util.getAllTripsByCarrier(prevoznik);
		session.setAttribute("allTrips", trips);

		RequestDispatcher disp = req
		.getRequestDispatcher("WebContent/timeTable.jsp");
		disp.forward(req, resp);
		return;
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		doPost(req, resp);
	}
}